package gminet.infra.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

public abstract class SimpleTransferObject extends TransferObject implements Serializable {

    private static final long serialVersionUID = -1396492395193188029L;

    private static Hashtable<Class<? extends SimpleTransferObject>, List<SimpleTransferObject>> htObjects = new Hashtable<Class<? extends SimpleTransferObject>, List<SimpleTransferObject>>();

    private String name;

    protected SimpleTransferObject(int i, String str) {

        super();
        setId(i);
        setName(str);
    }

    public String getName() {
        return name;
    }

    public void setName(String nome) {
        this.name = nome;
    }

    protected static SimpleTransferObject getObject(Class<? extends SimpleTransferObject> simpleClass, int codigo) {

        SimpleTransferObject simpleObject = null;
        List<SimpleTransferObject> objectsList = htObjects.get(simpleClass);
        if (objectsList != null) {
            for (SimpleTransferObject simpleTO : objectsList) {
                if (simpleTO.getId() == codigo) {
                    simpleObject = simpleTO;
                    break;
                }
            }
        }
        return simpleObject;
    }

    protected static void addObject(Class<? extends SimpleTransferObject> simpleClass, SimpleTransferObject simpleObject) {

        List<SimpleTransferObject> objectsList = htObjects.get(simpleClass);
        if (objectsList == null) {
            objectsList = new ArrayList<SimpleTransferObject>(10);
            htObjects.put(simpleClass, objectsList);
        }
        objectsList.add(simpleObject);
    }

    protected static List<? extends SimpleTransferObject> getList(Class<? extends SimpleTransferObject> simpleClass) {

        return htObjects.get(simpleClass);
    }

    @Override
    public boolean equals(Object simpleObject) {

        boolean isEqual = false;
        if (simpleObject instanceof SimpleTransferObject) {
            isEqual = ((SimpleTransferObject) simpleObject).getId() == getId();
        }
        return isEqual;
    }
}
